/**
 * com.dingjian.permission.service.impl.MenuServiceImpl.java
 */
package com.dingjian.permission.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingjian.base.dao.BaseDao;
import com.dingjian.basedata.service.impl.DataEntityService;
import com.dingjian.permission.dao.MenuDao;
import com.dingjian.permission.model.Menu;
import com.dingjian.permission.service.MenuService;

/**
 * @author FengMy
 * 
 * @since 2012-9-13
 */
@Component(value="menuService")
@Transactional
public class MenuServiceImpl extends DataEntityService<Menu> implements MenuService{
	@Autowired
	private MenuDao menuDao;
	@Override
	protected BaseDao getDao() {
		return menuDao;
	}
	
	@Override
	public void addEntity(Menu entity) {
		entity.setLongNumber(entity.getNumber());
		entity.setLevel(1);
		entity.setLeaf(true);
		if(entity.getParent()!=null){
			Menu parent = getEntityById(entity.getParent().getId());
			entity.setLongNumber(parent.getLongNumber() + "!" + entity.getNumber());
			entity.setLevel(parent.getLevel() + 1);
			parent.setLeaf(false);
			super.updateEntity(parent);
		}
		super.addEntity(entity);
	}
	
	@Override
	public void updateEntity(Menu entity) {
		entity.setLongNumber(entity.getNumber());
		entity.setLevel(1);
		entity.setLeaf(true);
		if(entity.getParent()!=null){
			Menu parent = getEntityById(entity.getParent().getId());
			entity.setLongNumber(parent.getLongNumber() + "!" + entity.getNumber());
			entity.setLevel(parent.getLevel() + 1);
			parent.setLeaf(false);
			super.updateEntity(parent);
		}
		super.updateEntity(entity);
	}
}
